using System;
using System.Runtime.Serialization;
using Ops.Mpc.Filters;


namespace Ops.Mpc.Collections
{
    /// <summary>
    /// Pager is used to page through a result set. It keeps track of the current page index, page count, and item cout.
    /// The pager also contains the filters that were used to create it so that the original result set can be recreated during fetches.
    /// </summary>
    [DataContract(Namespace = "Ops.Mpc")]
	public class Pager
    {
        #region Fields
        private int _count = 0;
        private int _pageSize = 10;
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public Pager()
        {
            PageSize = 10;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Set the page pointer to the next page of results
        /// </summary>
        public void Next()
        {
            if(this.CurrentPageIndex < this.PageCount - 1)
                this.CurrentPageIndex++;
        }

        /// <summary>
        /// Set the page pointer to the previous page of results
        /// </summary>
        public void Previous()
        {
            if (this.CurrentPageIndex > 0)
                this.CurrentPageIndex--;
        }

        /// <summary>
        /// Set the page pointer to the last page of results
        /// </summary>
        public void Last()
        {
            this.CurrentPageIndex = this.PageCount - 1;
        }

        /// <summary>
        /// Set the page pointer to the first page of results
        /// </summary>
        public void First()
        {
            this.CurrentPageIndex = 0;
        }
        #endregion

        #region Properties

        /// <summary>
        /// The number of items to be returned per page
        /// </summary>
        [DataMember]
        public int PageSize
		{
            get
            {
                return this._pageSize;
            }

            set
            {

                this._pageSize = value;
                this.Count = this.Count;

            }
		}

        /// <summary>
        /// The current page index
        /// </summary>
        [DataMember]
		public int CurrentPageIndex
		{
			get;
			set;
		}

        /// <summary>
        /// The number of pages represented by the current result set
        /// </summary>
        [DataMember]
        public int PageCount
        {
            set;
            get;
        }

        /// <summary>
        /// The total number of items in the result set
        /// </summary>
        [DataMember]
        public int Count
        {
            set
            {
                this._count = value;

                int pageCount = (int)Math.Ceiling((double)this._count / (double)this._pageSize);

                if (pageCount < 1)
                    pageCount = 1;

                this.PageCount = pageCount;
            }

            get
            {
                return this._count;
            }
        }

        /// <summary>
        /// The filters that were used to create this result set
        /// </summary>
        [DataMember]
        public Filter[] Filters
        {
            get;
            set;
        }


  
        #endregion
    }
}
